//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================



using System.Collections.Generic;
using System.ComponentModel;

namespace GuidanceExplorer.Model
{
    /// <summary>
    /// Represents a view which is a collection of checklist items from any library in the model.
    /// </summary>
    public class Checklist : View
    {
       // private View view;
		private Dictionary<string, bool> _checkStatus;

		private Dictionary<string, string> _itemNotes;

        /// <summary>
        /// Factory.
        /// </summary>
        /// <returns>A new instance of a Checklist.</returns>
        public static new Checklist New()
        {
            return new Checklist();
        }

 
        /// <summary>
        /// Default private constructor.
        /// </summary>
        protected Checklist() 
        {
            _checkStatus = new Dictionary<string, bool>();
			_itemNotes = new Dictionary<string, string>();
        }

        public void SetCheck(string itemId, bool check)
        {
            if (_checkStatus.ContainsKey(itemId))
            {
                _checkStatus[itemId] = check;
            }
            else
            {
                _checkStatus.Add(itemId, check);
            }
        }
		/// <summary>
		/// Remove the Checklist from the container.
		/// </summary>
        public override void Remove(bool removeParentWhenEmpty)
		{
			if (ParentFolder == null)
			{				
				Container.Checklists.Remove(this.Caption);
			}
			else
			{
				ParentFolder.Checklists.Remove(this.Caption);
                if (removeParentWhenEmpty && ParentFolder.Views.Count == 0)
                {
                    ParentFolder.Remove();
                }
            }
		}

		public bool IsChecked(string itemId)
        {
            if (_checkStatus.ContainsKey(itemId))
            {
                return _checkStatus[itemId];
            }
            return false;
        }

		// Gets the notes assosiated with a checklist
		public string GetItemNotes(string itemId)
		{
			if (_itemNotes.ContainsKey(itemId))
			{
				return _itemNotes[itemId];
			}
			return "";
		}

        [Browsable(false)]
        public override ColumnCollection Columns
        {
            get
            {

                return _columns;
            }
            set
            {
                _columns = value;
                AddStateColumns();
            }
        }

        public void ResetNotes()
        {
            _itemNotes.Clear();
        }

        public void ResetChecks()
        {
            _checkStatus.Clear();
        }

        private void AddStateColumns()
        {

            Column checkCol = Column.New();
            checkCol.Source = "Checked";
            checkCol.Caption = "";
            checkCol.Position = 0;
            checkCol.Visible = true;
            checkCol.Editable = true;
            Field state = Field.New();
            state.Type = FieldType.Check;
            checkCol.Field = state;
            if (!_columns.ContainsKey(checkCol.Source))
            {
                //Shift the columns position to place the checked column first
                foreach (Column col in _columns.Values)
                {
                    col.Position++;
                }
                //Add checked column
                _columns.Add(checkCol.Source, checkCol);
            }                      

            Column noteCol = Column.New();
            noteCol.Source = "Note";
            noteCol.Caption = "Note";
            noteCol.Position = _columns.Count+1;
            noteCol.Visible = true;
            noteCol.Editable = true;
            if (!_columns.ContainsKey(noteCol.Source))
            {
                _columns.Add(noteCol.Source, noteCol);
            }
        }

		// Sets the notes assosiated with a checklist
		public void SetItemNotes(string itemId, string notes)
		{
			if (_itemNotes.ContainsKey(itemId))
			{
				_itemNotes[itemId] = notes;
			}
			else
			{
				_itemNotes.Add(itemId, notes);
			}
		}
    }
}

